﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML dir=ltr XMLNS:MSHelp = "http://msdn.microsoft.com/mshelp" xmlns:ddue = 
"http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink = 
"http://www.w3.org/1999/xlink" xmlns:tool = "http://www.microsoft.com/tooltip" 
XMLNS:[default] http://ddue.schemas.microsoft.com/authoring/2003/5 = 
"http://ddue.schemas.microsoft.com/authoring/2003/5"><HEAD><TITLE>Echo Sample</TITLE>
<META content="text/html; CHARSET=utf-8" http-equiv=Content-Type></META>
<META name=save content=history></META>
<LINK rel=stylesheet type=text/css 
href="../../../CommonFiles/Classic.css"></LINK>

<META name=GENERATOR content="MSHTML 8.00.6001.18783">
<style type="text/css">
.style1 {
				font-family: monospace;
				font-size: 100%;
				color: #000000;
}
.style2 {
				font-family: monospace;
				font-size: small;
				color: #000000;
}
</style>
</HEAD>
<BODY>
<DIV id=header>
<TABLE id=topTable width="100%">
  <TBODY>
  <TR id=headerTableRow1>
    <TD align=left><SPAN id=runningHeaderText></SPAN></TD></TR>
  <TR id=headerTableRow2>
    <TD align=left><SPAN id=nsrTitle>Echo Sample</SPAN></TD></TR>
  <TR id=headerTableRow3>
    <TD></TD></TR></TBODY></TABLE>
</DIV>
<DIV id=mainSection>
<DIV id=mainBody>
<DIV id=allHistory class=saveHistory onload="loadAll()" 
onsave="saveAll()"></DIV>
<P>This sample demonstrates how to use the Windows Azure 
Service Bus and the <B>NetTcpRelayBinding</B> binding. See the Service Bus 
documentation for more information about the Service Bus before exploring the 
samples. </P>
<P>This sample shows a simple service and client that communicate through the 
Service Bus. When the service application is started, it asks for your 
issuer credentials and opens an endpoint on the Service 
Bus. After it is opened, this endpoint has a well-known URI on the Service Bus 
and is reachable from anywhere, whether or not your computer resides behind a 
firewall or Network Address Translation (NAT). Clients that access the endpoints 
must have permission to talk to the endpoint. Therefore, the client application 
also asks for your issuer credentials, authenticates with the Access Control 
service, and acquires an access token that proves to the Service Bus 
infrastructure that the client is authorized to access the endpoint. After the 
client is connected, you can type messages into the client application, which 
will be echoed back by the service. </P>
<H2 class=heading>Prerequisites</H2>
<DIV id=sectionSection0 class=section><content 
xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
<P xmlns="">If you haven't already done so, please read the release notes 
document that explains how to sign up for a Windows Azure 
account and how to configure your environment.</P>
				</content></DIV>
<H2 class=heading>Echo Service</H2>
<DIV id=sectionSection1 class=section><content 
xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
<P xmlns="">The service implements a simple contract with a single operation 
named <CODE>Echo</CODE>. The Echo service accepts a string and echoes the string 
back. </P>
<DIV class=code xmlns=""><SPAN codeLanguage="CSharp">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>C#&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE><SPAN codeLanguage="CSharp"> </SPAN>[ServiceBehavior(Name = &quot;EchoService&quot;, Namespace = &quot;http://samples.microsoft.com/ServiceModel/Relay/&quot;)]
class EchoService : IEchoContract
{
   public string Echo(string text)
   {
      Console.WriteLine(&quot;Echoing: {0}&quot;, text);
      return text; 
   }
}</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">The service configuration is shown below:</P>
<DIV class=code xmlns=""><SPAN codeLanguage="xml">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>Xml&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;configuration&gt;
  &lt;system.serviceModel&gt;
    &lt;services&gt;
      &lt;!-- Application Service --&gt;
      &lt;service name="Microsoft.ServiceBus.Samples.EchoService"&gt;
        &lt;endpoint contract="Microsoft.ServiceBus.Samples.IEchoContract"
      binding="netTcpRelayBinding" /&gt;
      &lt;/service&gt;
    &lt;/services&gt;
  &lt;/system.serviceModel&gt;
&lt;/configuration&gt;</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">Assuming you have some prior knowledge of the structure of 
configuration files for the Windows Communication Foundation (WCF), you will 
find that the configuration file is similar to any other WCF configuration file, 
with the exception that the configured service endpoint refers to a 
<CODE>NetTcpRelayBinding</CODE> binding, which is not part of the .NET 
Framework. </P>
<P xmlns="">The <B>NetTcpRelayBinding</B> binding is one of the bindings 
introduced with the Service Bus, and the respective configuration element 
    needs to be added to the application configuration file. </P>
<P xmlns="">The code that hosts the service (in Program.cs or Program.vb) 
contains two key aspects of interest:</P>
<UL xmlns="">
  <LI class=unordered>How does addressing work?<BR><BR>
  <LI class=unordered>How do authentication and authorization work? 
<BR><BR></LI></UL>
<DIV class=code xmlns=""><SPAN codeLanguage="CSharp">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>C#&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE class="style1">Console.Write(&quot;Your Service Namespace: &quot;);
string serviceNamespace = Console.ReadLine();
Console.Write(&quot;Your Issuer Name: &quot;);
string issuerName = Console.ReadLine();
Console.Write(&quot;Your Issuer Secret: &quot;);
string issuerSecret = Console.ReadLine();

// create the service URI based on the service namespace
Uri address = ServiceBusEnvironment.CreateServiceUri(&quot;sb&quot;, serviceNamespace, &quot;EchoService&quot;);

// create the credentials object for the endpoint
TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
sharedSecretServiceBusCredential.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret);            

// create the service host reading the configuration
ServiceHost host = new ServiceHost(typeof(EchoService), address);

// create the ServiceRegistrySettings behavior for the endpoint
IEndpointBehavior serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Public);

// add the Service Bus credentials to all endpoints specified in configuration
foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
{
   endpoint.Behaviors.Add(serviceRegistrySettings);
   endpoint.Behaviors.Add(sharedSecretServiceBusCredential);
}

// open the service
host.Open();

Console.WriteLine(&quot;Service address: &quot; + address);
Console.WriteLine(&quot;Press [Enter] to exit&quot;);
Console.ReadLine();

// close the service
host.Close();</PRE>
	</TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">To implement a service that listens to the Service Bus, the Service 
Bus service must be able to verify that the owner of the listening service is 
authorized to do so. </P>
<P xmlns="">Authentication and authorization are both performed by the 
Access Control service. To make these steps simple, the 
<B>Microsoft.ServiceBus</B> namespace contains a set of transport client 
credential helpers that automatically handle the acquisition of the required 
security tokens. </P>
<P xmlns="">The credential used in this example is a simple user name/password 
credential that is backed by the issuer credentials you set up when signing up 
for a Windows Azure account. To associate a listener endpoint with its 
Service Bus credentials, you must add a <B>TransportClientEndpointBehavior</B> 
behavior to the respective endpoint behavior collection. If your service exposes 
multiple endpoints through the Relay, you can add the same behavior instance to 
all those endpoints.</P>
<P xmlns="">Each service namespace automatically owns a branch of the 
Service Bus global namespace. Your service namespace branch is rooted at:</P>
<DIV class=code xmlns=""><SPAN codeLanguage="other">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE>[scheme]://&lt;service-namespace&gt;.servicebus.windows.net/ </PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">in which <CODE>[scheme]</CODE> is either <CODE>sb</CODE> (as in this 
example) or <CODE>http</CODE> or <CODE>https</CODE>. The namespace owner can 
subdivide that namespace and organize services onto that namespace as needed and 
define rules in Access Control to guard access to branches of the 
namespace. </P>
<P xmlns="">The preceding code prompts for the issuer credential and then 
constructs the listening URI using that information. The static 
<CODE>ServiceBusEnvironment.CreateServiceUri</CODE> function is provided to help 
construct the URI using the correct format and domain name. It is strongly 
recommended that you use this function instead of building the URI from scratch 
because the URI construction logic and format might change in future releases. 
At present, the resulting listener URI is 
<CODE>scheme://&lt;service-namespace&gt;.servicebus.windows.net/EchoService/</CODE>. These 
URIs can be used as absolute listening URIs for endpoints, or as the base 
address of the service.</P></content></DIV>
<H2 class=heading>Echo Service Client</H2>
<DIV id=sectionSection2 class=section><content 
xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
<P xmlns="">The client application mirrors the service in terms of its 
configuration.</P>
<DIV class=code xmlns=""><SPAN codeLanguage="xml">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>Xml&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;configuration&gt;
  &lt;system.serviceModel&gt;
    &lt;client&gt;
      &lt;!-- Application Endpoint --&gt;
      &lt;endpoint name="RelayEndpoint"
                contract="Microsoft.ServiceBus.Samples.IEchoContract"
                binding="netTcpRelayBinding"/&gt;
    &lt;/client&gt;
  &lt;/system.serviceModel&gt;
&lt;/configuration&gt;</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">When it runs, the client prompts for the service namespace credentials, 
creates a channel to the service, and sends requests by calling the 
<CODE>Echo</CODE> operation. After the interaction is complete, the client 
closes the channel and exits.</P>
<DIV class=code xmlns=""><SPAN codeLanguage="CSharp">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>C#&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE class="style1">Console.Write(&quot;Your Service Namespace: &quot;);
string serviceNamespace = Console.ReadLine();
Console.Write(&quot;Your Issuer Name: &quot;);
string issuerName = Console.ReadLine();
Console.Write(&quot;Your Issuer Secret: &quot;);
string issuerSecret = Console.ReadLine();

// create the service URI based on the service namespace
Uri address = ServiceBusEnvironment.CreateServiceUri(&quot;sb&quot;, serviceNamespace, &quot;EchoService&quot;);

// create the credentials object for the endpoint
TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
sharedSecretServiceBusCredential.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret);            

// create the channel factory loading the configuration
ChannelFactory&lt;IEchoChannel&gt; channelFactory = new ChannelFactory&lt;IEchoChannel&gt;(&quot;RelayEndpoint&quot;, new EndpointAddress(serviceUri));

// apply the Service Bus credentials
channelFactory.Endpoint.Behaviors.Add(sharedSecretServiceBusCredential);

// create and open the client channel
IEchoChannel channel = channelFactory.CreateChannel();
channel.Open();

Console.WriteLine(&quot;Enter text to echo (or [Enter] to exit):&quot;);
string input = Console.ReadLine();
while (input != String.Empty)
{
   try
   {
      Console.WriteLine(&quot;Server echoed: {0}&quot;, channel.Echo(input));
   }
   catch (Exception e)
   {
      Console.WriteLine(&quot;Error: &quot; + e.Message);
   }
   input = Console.ReadLine();
}

channel.Close();
channelFactory.Close();</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
    </content></DIV>
<H2 class=heading>Running the Sample</H2>
<DIV id=sectionSection4 class=section><content 
xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
<P xmlns="">To run the sample, build the solution in Visual Studio or from the 
command line, then run the two resulting executable files. Run the service 
first, and then run the client. Both programs prompt for your service namespace and the issuer credentials. For the issuer secret, 
be sure to use the "Default Issuer Key" value from the portal, rather than one of the management keys.</P>
<P xmlns="">When the service and the client are running, you can start typing 
messages into the client application. These messages are echoed by the 
service.</P>
<P xmlns=""><B>Expected Output - Service</B></P>
<DIV class=code xmlns=""><SPAN codeLanguage="other">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TD colSpan=2><PRE class="style2">Your Service Namespace: &lt;service-namespace&gt;
Your Issuer Name: &lt;issuer-name&gt;
Your Issuer Secret: &lt;issuer-secret&gt;
Service address: sb://&lt;service-namespace&gt;.servicebus.windows.net/services/EchoService/
Press [Enter] to exit</PRE>
	</TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns=""><B>Expected Output - Client</B></P>
<DIV class=code xmlns=""><SPAN codeLanguage="other">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TD colSpan=2><PRE><SPAN codeLanguage="other">Your Service Namespace: &lt;service-namespace&gt;
Your Issuer Name: &lt;issuer-name&gt;
Your Issuer Secret: &lt;issuer-secret&gt;
Enter text to echo (or [Enter] to exit): Hello, World!
Server echoed: Hello, World!</SPAN></PRE></TD></TR></TBODY></TABLE></SPAN></DIV></content></DIV><!--[if gte IE 5]><tool:tip 
avoidmouse="false" element="languageFilterToolTip"></tool:tip><![endif]--></DIV>
<P xmlns="">
    <hr /> 
    Did you find this information useful?
    <a href="http://go.microsoft.com/fwlink/?LinkID=155664">
        Please send your suggestions and comments about the documentation.

    </a></P></DIV></BODY></HTML>
